#include "IObject.h"
#include <iostream>
using namespace std;

set<IObject *> IObject::objects;

IObject::IObject()
{
    droped = false;
    objects.insert(this);
}

IObject::~IObject()
{
    objects.erase(this);
}
set<IObject *> IObject::getObjects()
{
    return objects;
}

set<BlockObject *> BlockObject::blocks;

BlockObject::BlockObject(Block block) : block(block)
{
    out_of_edge = false;
    blocks.insert(this);
}

set<BlockObject *> BlockObject::getBlocks()
{
    return blocks;
}

BlockObject::~BlockObject()
{
    blocks.erase(this);
}

set<BallObject *> BallObject::balls;

BallObject::BallObject(Ball ball) : ball(ball)
{
    balls.insert(this);
}
BallObject::~BallObject()
{
    balls.erase(this);
}

set<BallObject *> BallObject::getBalls()
{
    return balls;
}

std::set<BlockObject *> getCollidedBlocks(const BallObject &ball)
{
    set<BlockObject *> blocks = BlockObject::getBlocks();
    set<BlockObject *> collided;
    for (set<BlockObject *>::iterator i = blocks.begin(); i != blocks.end(); i++)
    {
        BlockObject *v = *i;
        if (checkCollision(v->block, v->pos, ball.ball, ball.pos))
            collided.insert(v);
    }
    return collided;
}

void drawObjects(Shader shader)
{
    set<IObject *> objects = IObject::getObjects();
    for (set<IObject *>::iterator i = objects.begin(); i != objects.end(); i++)
    {
        IObject *obj = *i;
        obj->draw(shader);
    }
}
void updateObjects(float dt)
{
    set<IObject *> objects = IObject::getObjects();
    for (set<IObject *>::iterator i = objects.begin(); i != objects.end(); i++)
    {
        IObject *obj = *i;
        obj->update(dt);
    }
    for (set<IObject *>::iterator i = objects.begin(); i != objects.end(); i++)
    {
        IObject *obj = *i;
        if (obj->droped)
            delete obj;
    }
}
void deinitObjects()
{
    set<IObject *> objects = IObject::getObjects();
    for (set<IObject *>::iterator i = objects.begin(); i != objects.end(); i++)
    {
        IObject *obj = *i;
        delete obj;
    }
}